import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    name: "layout",
    component: () => import("@/views/layout/index.vue"),
    redirect: "home",
    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/home/index.vue"),
        meta: {
          keepAlive: false,
        },
        children: [],
      },
      {
        path: "/subjectInfo",
        name: "subjectInfo",
        component: () => import("@/views/children/subjectInfo/subjectInfo.vue"),
        redirect: "intro",
        meta: {
          keepAlive: false,
        },
        children: [
          {
            path: "/intro",
            name: "intro",
            component: () =>
              import("@/views/children/subjectInfo/children/intro.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/distribution",
            name: "distribution",
            component: () =>
              import("@/views/children/subjectInfo/children/distribution.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/institution",
            name: "institution",
            component: () =>
              import("@/views/children/subjectInfo/children/institution.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/personnel",
            name: "personnel",
            component: () =>
              import("@/views/children/subjectInfo/children/personnel.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/food",
            name: "food",
            component: () =>
              import("@/views/children/subjectInfo/children/food.vue"),
            meta: {
              keepAlive: false,
            },
          },
        ],
      },
      {
        path: "/integrity",
        name: "integrity",
        component: () => import("@/views/children/integrity/index.vue"),
        redirect: "commend",
        meta: {
          keepAlive: false,
        },
        children: [
          {
            path: "/commend",
            name: "commend",
            component: () =>
              import("@/views/children/integrity/children/commend.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/publicity",
            name: "publicity",
            component: () =>
              import("@/views/children/integrity/children/publicity.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/viewDetail",
            name: "viewDetail",
            component: () =>
              import("@/views/children/integrity/children/viewDetail.vue"),
            meta: {
              keepAlive: false,
            },
          },
        ],
      },
      {
        path: "/shopQuery",
        name: "shopQuery",
        component: () => import("@/views/children/shopQuery/index.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "/pricesManager",
        name: "pricesManager",
        component: () => import("@/views/children/pricesManager/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/trad",
        name: "trad",
        component: () => import("@/views/children/trad/index.vue"),
        redirect: "tradinquire",
        meta: {
          keepAlive: false,
        },
        children: [
          {
            path: "/tradinquire",
            name: "tradinquire",
            component: () =>
              import("@/views/children/trad/children/tradinquire.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/suyuaninquire",
            name: "suyuaninquire",
            component: () =>
              import("@/views/children/trad/children/suyuaninquire.vue"),
            meta: {
              keepAlive: false,
            },
          },
        ],
      },
      {
        path: "/knowledge",
        name: "knowledge",
        component: () => import("@/views/children/knowledge/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/detection",
        name: "detection",
        component: () => import("@/views/children/detection/index.vue"),
        redirect: "publicity1",
        meta: {
          keepAlive: false,
        },
        children: [
          {
            path: "/publicity1",
            name: "publicity1",
            component: () =>
              import("@/views/children/detection/children/publicity.vue"),
            meta: {
              keepAlive: false,
            },
          },
          {
            path: "/statistics",
            name: "statistics",
            component: () =>
              import("@/views/children/detection/children/statistics.vue"),
            meta: {
              keepAlive: false,
            },
          },
        ],
      },
      {
        path: "/complain",
        name: "complain",
        component: () => import("@/views/children/complain/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
    ],
  },
  {
    path: "/shopDetail",
    name: "shopDetail",
    component: () => import("@/views/children/shopQuery/detail.vue"),
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/codeInput",
    name: "codeInput",
    component: () => import("@/views/children/codeInput/index.vue"),
    meta: {
      keepAlive: false,
    },
  },
];

export const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
});

router.beforeEach((to, from, next) => {
  if (to.path == "/home" && localStorage.getItem('weiyima')) {
    console.log('to');
    next()
  } else if (to.path !== "/codeInput" && !localStorage.getItem('weiyima')) {
    console.log(to);
    console.log('from');
    // router.push("/codeInput");
    // next('/codeInput')
    next({ path: '/codeInput' })
  } else {
    next()

  }
});

